/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: nn
 * Date: 2025-04-21
 * Time: 10:39
 */
public class Test {
    public static void main(String[] args) {
        BinaryTree binaryTree = new BinaryTree();
        BinaryTree.TreeNode root = binaryTree.createTree();
        //System.out.println(root.val);
        binaryTree.preOrder(root);
        System.out.println();
        binaryTree.inOrder(root);
        System.out.println();
        binaryTree.postOrder(root);
        System.out.println();
        System.out.println("层序遍历：");
        binaryTree.levelOrder(root);
        System.out.println();
    }

    /*
    6. 二叉树的构建及遍历。OJ链接
    class TreeNode{
    public char val;
    public TreeNode left;
    public TreeNode right;

    public TreeNode(char val){
        this.val = val;
    }
}
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextLine()) { // 注意 while 处理多个 case
            String str = in.nextLine();
            TreeNode root = createTree(str);
            //中序遍历
            inorder(root);
        }
    }

    public static int i = 0;
    private static TreeNode createTree(String str) {
        TreeNode root = null;
        char ch = str.charAt(i);
        if(ch != '#') {
           root = new TreeNode(ch);
           i++;
           root.left = createTree(str);
           root.right = createTree(str);
        }else {
            i++;
        }
        return root;
    }

    private static void inorder(TreeNode  root) {
        if(root == null) {
            return;
        }
        inorder(root.left);
        System.out.print(root.val+" ");
        inorder(root.right);
    }
}

    */

    public static void main1(String[] args) {
        BinaryTree binaryTree = new BinaryTree();
        BinaryTree.TreeNode root = binaryTree.createTree();
//        System.out.println(root.val);
        binaryTree.preOrder(root);//进行前序遍历
        System.out.println();

        binaryTree.inOrder(root);//进行中序遍历
        System.out.println();

        binaryTree.postOrder(root);//进行后序遍历
        System.out.println();

        System.out.print("树中节点的个数：");// 获取树中节点的个数
        System.out.println(binaryTree.siz2(root));

        System.out.print("叶子节点的个数：");//获取叶子节点的个数
        binaryTree.getLeafNodeCount(root);
        System.out.println(binaryTree.leafSize);
        System.out.print("叶子节点的个数：");
        System.out.println(binaryTree.getLeafNodeCount2(root));

        System.out.print("第K层节点的个数：");// root的第k层有多少个节点
        System.out.println(binaryTree.getKLevelNodeCount(root,3));

        System.out.print("树的高度：");
        System.out.println(binaryTree.getHeight2(root));

        System.out.print("查找节点：");
        BinaryTree.TreeNode ret = binaryTree.find(root, 'G');
        System.out.println(ret.val);
        //若是没找到会出现空指针异常    NullPointerException
        System.out.print("查找节点：");
        BinaryTree.TreeNode ret1 = binaryTree.find(root, 'p');
        System.out.println(ret1.val);



    }


}
